/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.basedata.area.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BaseAreasMapper;
import com.nmf.dao.gen.BaseCitiesMapper;
import com.nmf.dao.gen.bean.BaseAreas;
import com.nmf.dao.gen.bean.BaseAreasExample;
import com.nmf.dao.gen.bean.BaseCities;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;

/**
 * 行政区域Service
 * @author 北冥
 * @version 2018-12-21
 */
@Service
@Transactional(readOnly = true)
public class BaseAreasService{

	@Autowired
	BaseAreasMapper baseAreasMapper;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BaseCitiesMapper baseCitiesMapper;
	public BaseAreas get(String no) {
		return baseAreasMapper.selectByPrimaryKey(no);
	}
	
	public List<BaseAreas> findList(BaseAreas baseAreas) {
		BaseAreasExample baseAreasExample = new BaseAreasExample();
		BaseAreasExample.Criteria criteria = baseAreasExample.createCriteria();
		
		return baseAreasMapper.selectByExample(baseAreasExample);
	}
	
	public Page<BaseAreas> findPage(Page<BaseAreas> page, BaseAreas baseAreas) {
		BaseAreasExample baseAreasExample = new BaseAreasExample();
		BaseAreasExample.Criteria criteria = baseAreasExample.createCriteria();
		
		baseAreasExample.setPage(page);
		
		List<BaseAreas> dlist = baseAreasMapper.selectByExample(baseAreasExample);
		page.setList(dlist);
		page.setCount(baseAreasMapper.countByExample(baseAreasExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BaseAreas baseAreas) {
		if(StringUtils.isNotBlank(baseAreas.getCityId())) {
			String cityId = baseAreas.getCityId();
			
			BaseCities baseCities = baseCitiesMapper.selectByPrimaryKey(cityId);
			baseAreas.setCityName(baseCities.getCityName());
		}
		if(StringUtils.isNotEmpty(baseAreas.getId())&&get(baseAreas.getId())!=null) {
			baseAreasMapper.updateByPrimaryKeySelective(baseAreas);
		}else {
			Date date = new Date();
			baseAreas.setCreateDate(date);
			baseAreas.setId(idGenerater.genClsNo("baseAreas"));
			baseAreas.setUpdateDate(date);
			baseAreasMapper.insertSelective(baseAreas);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BaseAreas baseAreas) {
		baseAreasMapper.deleteByPrimaryKey(baseAreas.getId());
	}
	
}